履歴: ソフトウェア/rails/ActiveSupport_TestCase + shoulda によるテスト

各行は、その日時に書かれた内容です。追加・削除は、1 つ前の版との行数の差です (全 13 版)。「差分」は比較する版、「基準」は比べる相手の版を選びます

日時タイトル文字数追加削除内容差分基準
2014/6/2 9:53:59ソフトウェア/rails/ActiveSupport_TestCase + shoulda によるテスト 最新版39,445+1 -1表示差分—
2014/6/2 9:53:59ソフトウェア/rails/ActiveSupport_TestCase + shoulda によるテスト 39,451+6 -1表示差分選択
2014/5/30 14:33:33ソフトウェア/rails/ActiveSupport_TestCase + shoulda によるテスト 39,363+2 -0表示差分選択
2014/5/30 14:32:24ソフトウェア/rails/ActiveSupport_TestCase + shoulda によるテスト 39,288+116 -17表示差分選択
2014/5/30 7:58:45ソフトウェア/rails/ActiveSupport_TestCase + shoulda によるテスト 36,014+229 -0表示差分選択
2014/5/26 4:31:48ソフトウェア/rails/ActiveSupport_TestCase + shoulda によるテスト 29,846+11 -4表示差分選択
2014/5/26 4:31:48ソフトウェア/rails/ActiveSupport_TestCase + shoulda によるテスト 29,608+30 -0表示差分選択
2014/5/24 1:25:48ソフトウェア/rails/ActiveSupport_TestCase + shoulda によるテスト 28,564+1 -1表示差分選択
2014/5/23 17:42:25ソフトウェア/rails/ActiveSupport_TestCase + shoulda によるテスト 27,825+163 -0表示差分選択
2014/5/23 13:25:19ソフトウェア/rails/ActiveSupport_TestCase + shoulda によるテスト 21,221+174 -2表示差分選択
2014/5/23 3:13:32ソフトウェア/rails/ActiveSupport_TestCase + shoulda によるテスト 15,654+253 -88表示差分選択
2014/5/22 14:25:52ソフトウェア/rails/ActiveSupport_TestCase + shoulda によるテスト 12,435+167 -47表示表示中選択
2014/5/22 13:28:26ソフトウェア/rails/ActiveSupport_TestCase + shoulda によるテスト 9,057— —表示—基準

基準: ソフトウェア/rails/ActiveSupport_TestCase + shoulda によるテスト (2014/5/22 13:28:26) → 比較: ソフトウェア/rails/ActiveSupport_TestCase + shoulda によるテスト (2014/5/22 14:25:52)

@@ -1,17 +1,25 @@
[[公開メモ]]
-* ActionController::TestCase + shoulda による Ruby on Rails コントローラのテスト [#ge5f5cd1]
+#contents
-- ActionController::TestCase は Rails 標準のコントローラテストフレームワーク
-- [[shoulda>https://github.com/thoughtbot/shoulda]] は読みやすいテストケースを書くための gem
+* ActiveSupport::TestCase + shoulda による Ruby on Rails のテスト [#ge5f5cd1]
-これらを使ってコントローラをテストするためのノウハウをメモとして残したい
+- ActiveSupport::TestCase は Rails 標準のテストフレームワーク
+- [[shoulda>https://github.com/thoughtbot/shoulda]] は読みやすいテストケースを書くための拡張
+これらを使って Rails アプリケーションをテストするためのノウハウをメモとして残したい
+
* いろいろ検討中 [#rc1df26c]
まだちゃんとしたテストは書き始めたばかりなので全然やり方が定まってません。
このレベルの物をこんなところに晒すのも何なのですが、とりあえずメモとして書いちゃいます。
+** コントローラのテスト [#m9c4f2cd]
+
+途中ロケールの処理が入っていますが、そのあたりの構成は
+[[ソフトウェア/rails/国際化]] に書いた通りです。
+
+test/controllers/account_controller_test.rb
LANG:ruby(linenumber)
#coding: utf-8
require 'test_helper'
@@ -49,7 +57,7 @@
# - 本来の目的からずれまくってるので完全に反則技
# - 初期化し忘れると直前のテストケースの値が残ってしまうのも問題
# - その代わり、どこからでもアクセスできるのがかなり便利?
-
+
@@valid_account_params = {
name: "Foo Baa",
email: "account@example.com",
@@ -102,45 +110,6 @@
cookies.clear
end
- # shoulda の redirect_to では、
- # should redirect_to account_path
- # should redirect_to account_path(action: :edit)
- # should redirect_to url_for(controller: :account, action: :edit)
- # などとは書けないので、かわりに
- # should redirect_to("account_path"){ account_path }
- # should redirect_to("account_path(action: :edit)"){ account_path(action: :edit) }
- # should redirect_to("controller: :account, action: :edit"){
- # @controller.url_for(controller: :account, action: :edit) }
- # としても良いのだけれど、より簡単に
- # should redirect_to_path :account
- # should redirect_to_path :account, action: :edit
- # should redirect_to_path controller: :account, action: :edit
- # などと書けるようにする
- def self.redirect_to_path(*arg)
- if arg.first.is_a? Symbol
- redirect_to_path_arg = arg.dup
- redirect_to_path_symbol = :"#{redirect_to_path_arg.shift}_path"
- if redirect_to_path_arg.empty?
- redirect_to(redirect_to_path_symbol){
- __send__(redirect_to_path_symbol)
- }
- else
- redirect_to_path_arg_s =redirect_to_path_arg.inspect.gsub(/\A\[|\]\z/,'')
- redirect_to_path_arg_s.gsub!(/\A\{|\}\z/,'') if redirect_to_path_arg.count == 1
- redirect_to("#{redirect_to_path_symbol}(#{redirect_to_path_arg_s})", &Proc.new {
- __send__(redirect_to_path_symbol, *redirect_to_path_arg)
- })
- end
- else
- redirect_to_path_arg = arg.dup
- redirect_to_path_arg_s =redirect_to_path_arg.inspect.gsub(/\A\[|\]\z/,'')
- redirect_to_path_arg_s.gsub!(/\A\{|\}\z/,'') if redirect_to_path_arg.count == 1
- redirect_to(redirect_to_path_arg_s){
- @controller.url_for *redirect_to_path_arg
- }
- end
- end
-
# 以下すべてを ja, en 両方のロケールで試す
[:ja,:en].each do |locale|
context "with locale #{locale}," do
@@ -166,7 +135,7 @@
get :home
}
should redirect_to_path :account_login
- should set_the_flash
+ should set_the_flash # login first
should 'set session_intended_path to requested path' do
# セッションキーは ApplicationController で
# @session_intended_path="hogehoge"
@@ -183,6 +152,7 @@
should "not create account" do
assert_equal 0, Account.count
end
+ should set_the_flash # already login
should render_template :new
end
@@ -193,6 +163,7 @@
should "create account" do
assert_equal 1, Account.count
end
+ should set_the_flash # account created
should redirect_to_path :account_login
should "send email to registered address" do
assert_not ActionMailer::Base.deliveries.empty?
@@ -203,10 +174,9 @@
end
- context "with autologin," do
+ context "with auto-login," do
setup {
- clear_all
@@account = Account.create!(
@@valid_account_params.merge(
password: Account.generate_new_password,
@@ -292,6 +262,156 @@
end
+* 独自拡張を読み込む [#xdb96442]
+
+test/test_helper.rb
+ LANG:ruby
+ ...
+
+ require 'test_i18n_translation_strictly'
+ Dir[File.expand_path('../shoulda_macros', __FILE__) << '/*.rb'].each do |file|
+ require file
+ end
+
+ ...
+
+
+** I18n.translate の翻訳の抜けを検出する [#ub2cc8f0]
+
+config/initializers/test_i18n_translation_strictly.rb
+ LANG:ruby(linenumber)
+ if ENV['RAILS_ENV'] == "test"
+
+ # 翻訳が見付からないときに例外を投げる
+ # I18n.exception_handler
+ # http://guides.rubyonrails.org/i18n.html
+ # これだけではモデル名やカラム名の翻訳は無くても
+ # エラーにならないので、
+ # ActiveRecord::model_name.human
+ # https://github.com/rails/rails/blob/master/activemodel/lib/active_model/translation.rb#L43
+ # ActiveRecord::human_attribute_name
+ # https://github.com/rails/rails/blob/master/activemodel/lib/active_model/naming.rb#L175
+ # にも手を入れる。locale == :en では、モデル名やカラム名を
+ # そのまま使えることも多いので、それ以外の時に翻訳が未設定
+ # であれば例外を投げる
+ #
+ # 後者2つについては関数のコードをほぼ丸々コピーしているので、
+ # 元のコードが変更されたらそれに合わせて変更しなければならない
+
+ module I18n
+ class JustRaiseExceptionHandler < ExceptionHandler
+ def call(exception, locale, key, options)
+ if exception.is_a?(MissingTranslation) and
+ key.to_s != 'i18n.plural.rule'
+ raise exception.to_exception
+ else
+ super
+ end
+ end
+ end
+ end
+ I18n.exception_handler = I18n::JustRaiseExceptionHandler.new
+
+ module ActiveModel
+ class Name
+ def human(options={})
+ return @human unless @klass.respond_to?(:lookup_ancestors) &&
+ @klass.respond_to?(:i18n_scope)
+
+ defaults = @klass.lookup_ancestors.map do |klass|
+ klass.model_name.i18n_key
+ end
+
+ defaults << options[:default] if options[:default]
+ # defaults << @human
+ defaults << @human if I18n.locale == :en
+
+ options = { scope: [@klass.i18n_scope, :models], count: 1, default: defaults }.merge!(options.except(:default))
+ I18n.translate(defaults.shift, options)
+ end
+ end
+ end
+
+ module ActiveModel
+ module Translation
+ def human_attribute_name(attribute, options = {})
+ options = { count: 1 }.merge!(options)
+ parts = attribute.to_s.split(".")
+ attribute = parts.pop
+ namespace = parts.join("/") unless parts.empty?
+ attributes_scope = "#{self.i18n_scope}.attributes"
+
+ if namespace
+ defaults = lookup_ancestors.map do |klass|
+ :"#{attributes_scope}.#{klass.model_name.i18n_key}/#{namespace}.#{attribute}"
+ end
+ defaults << :"#{attributes_scope}.#{namespace}.#{attribute}"
+ else
+ defaults = lookup_ancestors.map do |klass|
+ :"#{attributes_scope}.#{klass.model_name.i18n_key}.#{attribute}"
+ end
+ end
+
+ defaults << :"attributes.#{attribute}"
+ defaults << options.delete(:default) if options[:default]
+ # defaults << attribute.humanize
+ defaults << attribute.humanize if I18n.locale == :en
+
+ options[:default] = defaults
+ I18n.translate(defaults.shift, options)
+ end
+ end
+ end
+
+ end
+
+** should redirect_to_path [#r6fcffc4]
+
+test/shoulda_macros/action_controller_macros.rb
+ LANG:ruby(linenumber)
+ class ActionController::TestCase
+
+ # shoulda の redirect_to では、
+ # should redirect_to account_path
+ # should redirect_to account_path(action: :edit)
+ # should redirect_to url_for(controller: :account, action: :edit)
+ # などとは書けないので、かわりに
+ # should redirect_to("account_path"){ account_path }
+ # should redirect_to("account_path(action: :edit)"){ account_path(action: :edit) }
+ # should redirect_to("controller: :account, action: :edit"){
+ # @controller.url_for(controller: :account, action: :edit) }
+ # などと書かなければならない。より簡単に
+ # should redirect_to_path :account
+ # should redirect_to_path :account, action: :edit
+ # should redirect_to_path controller: :account, action: :edit
+ # などと書けるようにするのがこの関数
+ def self.redirect_to_path(*arg)
+ if arg.first.is_a? Symbol
+ redirect_to_path_arg = arg.dup
+ redirect_to_path_symbol = :"#{redirect_to_path_arg.shift}_path"
+ if redirect_to_path_arg.empty?
+ redirect_to(redirect_to_path_symbol){
+ __send__(redirect_to_path_symbol)
+ }
+ else
+ redirect_to_path_arg_s =redirect_to_path_arg.inspect.gsub(/\A\[|\]\z/,'')
+ redirect_to_path_arg_s.gsub!(/\A\{|\}\z/,'') if redirect_to_path_arg.count == 1
+ redirect_to("#{redirect_to_path_symbol}(#{redirect_to_path_arg_s})", &Proc.new {
+ __send__(redirect_to_path_symbol, *redirect_to_path_arg)
+ })
+ end
+ else
+ redirect_to_path_arg = arg.dup
+ redirect_to_path_arg_s =redirect_to_path_arg.inspect.gsub(/\A\[|\]\z/,'')
+ redirect_to_path_arg_s.gsub!(/\A\{|\}\z/,'') if redirect_to_path_arg.count == 1
+ redirect_to(redirect_to_path_arg_s){
+ @controller.url_for *redirect_to_path_arg
+ }
+ end
+ end
+
+ end
+
* コメント [#yfa709ad]
#article_kcaptcha